﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace Macds.Core.XML.Excel
{
    public class ExcelCellsCollection : ICollection<ExcelCell>
    {
        protected ArrayList _innerArray;

        public ExcelCellsCollection()
        {
            this._innerArray = new ArrayList();
        }

        public virtual ExcelCell this[int index]
        {
            get
            {
                return (ExcelCell)this._innerArray[index];
            }
            set
            {
                this._innerArray[index] = value;
            }
        }

        #region ICollection<ExcelCell> Members

        public void Add(ExcelCell item)
        {
            this._innerArray.Add(item);
        }

        public void Clear()
        {
            this._innerArray.Clear();
        }

        public bool Contains(ExcelCell item)
        {
            foreach (ExcelCell c in _innerArray)
            {
                if (c.UniqueId == item.UniqueId)
                {
                    return true;
                }
            }

            return false;
        }

        public void CopyTo(ExcelCell[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get
            {
                return this._innerArray.Count;
            }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(ExcelCell item)
        {
            bool result = false;

            for (int i = 0; i < _innerArray.Count; i++)
            {
                ExcelCell obj = (ExcelCell)_innerArray[i];

                if (obj.UniqueId == item.UniqueId)
                {
                    _innerArray.RemoveAt(i);
                    result = true;
                    break;
                }
            }

            return result;
        }

        #endregion

        #region IEnumerable<ExcelCell> Members

        public IEnumerator<ExcelCell> GetEnumerator()
        {
            return new ExcelCellsEnumerator(this);
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new ExcelCellsEnumerator(this);
        }

        #endregion
    }

    public class ExcelCellsEnumerator : IEnumerator<ExcelCell>
    {
        protected ExcelCellsCollection _collection;
        protected int index;
        protected ExcelCell _current;

        public ExcelCellsEnumerator() { }

        public ExcelCellsEnumerator(ExcelCellsCollection collection)
        {
            _collection = collection;
            index = -1;
            _current = default(ExcelCell);
        }

        #region IEnumerator<ExcelCell> Members

        public ExcelCell Current
        {
            get
            {
                return _current;
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            _collection = null;
            _current = default(ExcelCell);
            index = -1;
        }

        #endregion

        #region IEnumerator Members

        object IEnumerator.Current
        {
            get
            {
                return _current;
            }
        }

        public bool MoveNext()
        {
            if (++index >= _collection.Count)
            {
                return false;
            }
            else
            {
                _current = _collection[index];
            }

            return true;
        }

        public void Reset()
        {
            _current = default(ExcelCell);
            index = -1;
        }

        #endregion
    }
}
